package com.example.demo.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.demo.pojo.Competition;
import com.example.demo.result.Result;
import com.example.demo.service.CompetitionService;
import com.mysql.jdbc.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/admin")
//@CrossOrigin//解决跨域问题
public class AdminController {
    @Autowired
    private CompetitionService competitionService;

    //获取竞赛
    @PostMapping("listCom/{current}/{limit}")
    public Result getComList(@PathVariable Integer current, @PathVariable Integer limit, @RequestBody(required = false) Competition competition){
        Page<Competition> objectPage=new Page<>(current,limit);
        String cname=competition.getCname();
        QueryWrapper<Competition>qw=new QueryWrapper<>();
        if(!StringUtils.isNullOrEmpty(cname)){//不为空
            qw.like("cname",cname);
        }
        Page<Competition> page= (Page<Competition>) competitionService.page(objectPage,qw);
        return Result.success(page);
    }
    //发布竞赛
    @PostMapping("addCom")
    public Result addCom(@RequestBody Competition competition){
        boolean save=competitionService.save(competition);
        if(save){
            return Result.success("发布成功");
        }else{
            return Result.fail();
        }
    }
    //删除竞赛
    @PostMapping("deleteCom/{id}")
    public Result deleteById(@PathVariable Integer id){
        boolean b = competitionService.removeById(id);
        if(b){
            return Result.success();//前端提示信息已写
        }else{
            return Result.fail();
        }
    }
    //根据id获取竞赛信息
    @GetMapping("getCom/{id}")
    public Result getById(@PathVariable Integer id){
        Competition competition = competitionService.getById(id);
        return Result.success(competition);
    }
    //修改竞赛
    @PostMapping("updateCom")
    public Result updateCom(@RequestBody Competition competition){
        boolean b = competitionService.updateById(competition);
        if(b){
            return Result.success("修改成功");
        }
            return Result.fail();
    }
    //发布新闻
    @PostMapping("addNews")
    public Result addNews(@RequestBody Competition competition){
        competition.setStatus(1);
        boolean save=competitionService.updateById(competition);
        if(save){
            return Result.success("发布成功");
        }
            return Result.fail();
    }
    //修改新闻
    @PostMapping("updateNews")
    public Result updateNews(@RequestBody Competition competition){
        boolean b = competitionService.updateById(competition);
        if(b){
            return Result.success("修改成功");
        }
        return Result.fail();
    }
    //删除新闻
    @PostMapping("deleteNews")
    public Result deleteNews(@RequestBody Competition competition){
//        competition.setStatus(0);
        boolean b = competitionService.updateById(competition);//
        if (b){
            return Result.success();
        }
        return Result.fail();
    }
}
